import { showFailToast, showSuccessToast } from 'vant';

const copyToClipboard = (text: any) => {
  const textarea = document.createElement('textarea');
  textarea.style.position = 'fixed';
  textarea.style.opacity = '0';
  textarea.value = text;
  document.body.appendChild(textarea);
  textarea.select();
  document.execCommand('copy');
  document.body.removeChild(textarea);
  showSuccessToast('复制成功');
};

export const asyncCopyToClipboard = async (text: any) => {
  try {
    // 优先使用现代API
    if (navigator.clipboard) {
      await navigator.clipboard.writeText(text);
    } else {
      // 回退到旧版API
      const textarea = document.createElement('textarea');
      textarea.style.position = 'fixed';
      textarea.style.opacity = '0';
      textarea.value = text;
      document.body.appendChild(textarea);
      textarea.select();
      document.execCommand('copy');
      document.body.removeChild(textarea);
    }
  } catch (error) {
    showFailToast('复制失败，请手动复制');
  }
};

export default copyToClipboard;
